﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Trebuchet
{
    public partial class ShortcutForm : Form
    {
        public enum FormMode
        {
            Edit,
            Create
        }

        private FormMode mode;
        public FormMode Mode
        {
            get { return mode; }
            set { mode = value; }
        }
        private ShortcutCollection shortcuts;
        public ShortcutCollection Shortcuts
        {
            get { return shortcuts; }

            set { shortcuts = value; }
        }
        private Shortcut shortcut;
        public Shortcut Shortcut
        {
            get { return shortcut; }

            set { shortcut = value; }
        }
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public ShortcutForm()
        {
            InitializeComponent();
        }

        private void ShortcutForm_Load(object sender, EventArgs e)
        {
            if (shortcuts != null)
                RefreshShortcutList();

            if (shortcut != null)
                DisplayShortcut();

            if (mode == FormMode.Create)
            {
                ShortcutsList.Enabled = false;
                DeleteButton.Enabled = false;
                if (!string.IsNullOrEmpty(name))
                {
                    NameTextBox.Text = name;
                    CommandTextBox.Focus();
                }
            }
            else
            {
                ShortcutsList.Enabled = true;
                DeleteButton.Enabled = false;
                NameTextBox.Enabled = false;
                ArguementsTextBox.Enabled = false;
                CommandTextBox.Enabled = false;
                StartupPathTextBox.Enabled = false;
                NameTextBox.Text = string.Empty;
                ArguementsTextBox.Text = string.Empty;
                CommandTextBox.Text = string.Empty;
                StartupPathTextBox.Text = string.Empty;
                SaveButton.Enabled = false;
                CancelButton.Text = "&Close";
            }
        }

        private void DisplayShortcut()
        {
            NameTextBox.Text = shortcut.Name;
            CommandTextBox.Text = shortcut.Command;
            ArguementsTextBox.Text = shortcut.Arguements;
            StartupPathTextBox.Text = shortcut.StartupPath;
        }

        private void RefreshShortcutList()
        {
            ShortcutsList.Items.Clear();

            for (int i = 0; i < shortcuts.Count; i++)
                ShortcutsList.Items.Add(shortcuts[i].Name);
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            switch (mode)
            {
                case FormMode.Edit:
                    shortcut.Name = NameTextBox.Text;
                    shortcut.Command = CommandTextBox.Text;
                    shortcut.Arguements = ArguementsTextBox.Text;
                    shortcut.StartupPath = StartupPathTextBox.Text;
                    shortcuts.Save();
                    shortcut = null;
                    break;
                case FormMode.Create:
                    shortcut = new Shortcut();
                    shortcut.Name = NameTextBox.Text;
                    shortcut.Command = CommandTextBox.Text;
                    shortcut.Arguements = ArguementsTextBox.Text;
                    shortcut.StartupPath = StartupPathTextBox.Text;
                    shortcuts.Add(shortcut);
                    shortcuts.Save();
                    shortcut = null;
                    break;
                default:
                    break;
            }
            mode = FormMode.Edit;
            ShortcutForm_Load(this, null);
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ShortcutsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedName = ShortcutsList.SelectedItem.ToString();

            shortcut = shortcuts.GetShortcutByName(selectedName);

            DisplayShortcut();

            NameTextBox.Enabled = true;
            ArguementsTextBox.Enabled = true;
            CommandTextBox.Enabled = true;
            StartupPathTextBox.Enabled = true;
            SaveButton.Enabled = true;
            DeleteButton.Enabled = true;
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            shortcuts.Remove(shortcut);
            shortcuts.Save();
            mode = FormMode.Edit;
            ShortcutForm_Load(this, null);
        }
    }
}
